package hu.uniobuda.nik.openday.pagerindicator;

import java.io.File;
import java.io.IOException;

import hu.uniobuda.nik.openday.utils.Config;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import hu.uniobuda.nik.openday.view.ResizableImageView;
import android.widget.TextView;
import android.widget.TextView.BufferType;
import android.graphics.Bitmap;
import hu.uniobuda.nik.openday.R;

public class ViewPagerAdapter extends PagerAdapter{

	private View[] viewContainer;
	
	private AssetManager am;
	private String[] files;
	private String[] texts;
	private BitmapFactory.Options options;
	private Typeface font;
	
	public ViewPagerAdapter(Context context){
		
		this.am = context.getAssets();
		try {
			this.files = am.list(Config.ASSETS_PATH_LEARN_WHY);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		this.options = new BitmapFactory.Options();
		this.options.inPreferredConfig = Bitmap.Config.RGB_565;			
		this.options.inTempStorage = new byte[128 * 1024];
		this.texts = context.getResources().getStringArray(R.array.learn_why);
		font = Typeface.createFromAsset(context.getAssets(), Config.FONT); 
		viewContainer = new View[10];
		
		
		for (int i = 0; i < files.length; i++) {
			View view = View.inflate(context, R.layout.viewpager_template, null);
			TextView text = (TextView) view.findViewById(R.id.viewPagerText);
			text.setText(Html.fromHtml("<b>Mert</b> "+texts[i]), BufferType.SPANNABLE);
			text.setTypeface(font);
			ResizableImageView pics = (ResizableImageView)view.findViewById(R.id.viewPagerPic);
			try {				
				pics.setImageBitmap(BitmapFactory.decodeStream(am.open(Config.ASSETS_PATH_LEARN_WHY + File.separator + files[i]), null, options));								
			} catch (IOException e) { e.printStackTrace(); }
			viewContainer[i] = view;
		}		
	}	

	@Override
	public void destroyItem(View arg0, int arg1, Object arg2) {
		((ViewPager)arg0).removeView( (ViewGroup)arg2 );
	}

	@Override
	public void finishUpdate(View arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public int getCount() {
		 return viewContainer.length;
	}

	@Override
	public Object instantiateItem(View pager, int position) {
		((ViewPager)pager).addView( (ViewGroup)viewContainer[position], 0 );
		return (ViewGroup)viewContainer[position];
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0.equals( arg1 );
	}

	@Override
	public void restoreState(Parcelable arg0, ClassLoader arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public Parcelable saveState() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void startUpdate(View arg0) {
		// TODO Auto-generated method stub

	}

}
